package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.PowerUser;
import com.woniuxy.entity.dto.DepartmentDTO;
import com.woniuxy.entity.Department;
import com.woniuxy.entity.SubDepartment;
import com.woniuxy.entity.dto.SubDepartmentDTO;
import com.woniuxy.mapper.DeptMapper;
import com.woniuxy.mapper.PowerUserMapper;
import com.woniuxy.service.DeptService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private PowerUserMapper powerUserMapper;

    @Override
    public ResponseData<?> queryAllDept(PageData<DepartmentDTO> pageData) throws ParseException {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<Department> departments = deptMapper.queryAllDept(pageData.getParam());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Department department : departments) {
            try {
                // 解析原始日期字符串为Date对象
                Date date = dateFormat.parse(department.getCreatedTime());

                // 将Date对象格式化为yy-MM-dd格式的字符串
                String formattedDate = dateFormat.format(date);

                // 设置格式化后的日期字符串回Department对象
                department.setCreatedTime(formattedDate);
            } catch (Exception e) {
                // 处理解析日期时可能抛出的异常
                e.printStackTrace();
            }
        }

        PageInfo pageInfo = new PageInfo(departments);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> queryAllSubDept(Integer id) {
        List<SubDepartment> subDepartments = deptMapper.queryAllSubDept(id);
        return new ResponseData<>().ok(subDepartments);
    }

    @Override
    public ResponseData<?> addDept(Department department) {
        List<Department> departments = deptMapper.queryAllDept(new DepartmentDTO());
        for (Department department1 : departments) {
            if (department1.getDepartmentName().equals(department.getDepartmentName())){
                return new ResponseData<>().fail(ResponseEnum.FAIL);
            }
        }
        Long userId = UserContext.getUserId();
        PowerUser powerUser = powerUserMapper.queryPowerUserById(userId.intValue());
        department.setOperator(powerUser.getRealName());

        deptMapper.addDept(department);

        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> saveUpdateDept(Department department) {
        deptMapper.saveUpdateDept(department);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateStatus(Department department) {
        if (department.getDepartmentStatus().equals("0")){
            department.setDepartmentStatus("1");
            deptMapper.updateStatus(department);
            List<SubDepartment> subDepartments = deptMapper.queryAllSubDept(department.getId());
            if (subDepartments!= null && !subDepartments.isEmpty()){
                deptMapper.updateSubStatusByDeptId(department.getId(),"1");
                return new ResponseData<>().ok();
            }
            return new ResponseData<>().ok();
        }else if (department.getDepartmentStatus().equals("1")){
            department.setDepartmentStatus("0");
            deptMapper.updateStatus(department);
            List<SubDepartment> subDepartments = deptMapper.queryAllSubDept(department.getId());
            if (subDepartments!= null && !subDepartments.isEmpty()){
                deptMapper.updateSubStatusByDeptId(department.getId(),"0");
            }
            return new ResponseData<>().ok();
        }

        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }

    @Override
    public ResponseData<?> addSubDept(SubDepartment subDepartment) {
        SubDepartment sub = new SubDepartment();

        sub.setSubDepartmentName(subDepartment.getSubDepartmentName());
        sub.setRemarks(subDepartment.getRemarks());
        sub.setDepartmentId(subDepartment.getDepartmentId());
        Long userId = UserContext.getUserId();
        PowerUser powerUser = powerUserMapper.queryPowerUserById(userId.intValue());
        sub.setOperator(powerUser.getRealName());
        deptMapper.addSubDept(sub);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateSubDeptSave(SubDepartment subDepartment) {
        deptMapper.updateSubDeptSave(subDepartment);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateSubStatus(SubDepartment subDepartment) {
        if (subDepartment.getStatus().equals("0")){
            subDepartment.setStatus("1");
            deptMapper.updateSubStatus(subDepartment);
            return new ResponseData<>().ok();
        }else if (subDepartment.getStatus().equals("1")){
            subDepartment.setStatus("0");
            deptMapper.updateSubStatus(subDepartment);
            return new ResponseData<>().ok();
        }

        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }
}
